// https://leetcode.cn/problems/insert-delete-getrandom-o1/description/?envType=study-plan-v2&envId=top-interview-150

#include <cstdlib>
#include <ctime>

class RandomizedSet {
private:
    vector<int> data;
    unordered_map<int, int> dict;
public:
    RandomizedSet() {
        srand((unsigned)time(NULL));
    }
    
    bool insert(int val) {
        if (dict.count(val) > 0) return false;

        int index = data.size();
        data.push_back(val);
        dict[val] = index;
        return true;
    }
    
    bool remove(int val) {
        if (dict.count(val) <= 0) return false;

        int index = dict[val], num = data.back();
        data[index] = num;
        dict[num] = index;
        data.pop_back();
        dict.erase(val);
        return true;
    }
    
    int getRandom() {
        int index = rand() % data.size();
        return data[index];
    }
};

/**
 * Your RandomizedSet object will be instantiated and called as such:
 * RandomizedSet* obj = new RandomizedSet();
 * bool param_1 = obj->insert(val);
 * bool param_2 = obj->remove(val);
 * int param_3 = obj->getRandom();
 */